/*
    Copyright 2009 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package ot.s3;

import java.util.*;
import ot.*;

/** Hash tables of <tt>OPath</tt>s in buckets.
 *  This class maintains one table for each bucket in use<p>
 * 
 *  This class is not thread safe.
 * 
 *
 * @author Barnet Wagman
 */
public class BucketContents {
      
    private ManagedOps mops;
    
        /** Keyed by bucket name.
         * 
         */
    private HashMap<String,Bc> bcHt;
        
    public BucketContents(ManagedOps mops) {
        this.mops = mops;
        bcHt = new HashMap<String, Bc>();
    }
    
        /** Sync with the current contents of the S3 bucket.
         * 
         * @param mops
         * @param bucketName
         */
    void sync(String bucketName) throws OpFailedException {        
        Bc bc = getOrCreateBc(bucketName);
        bc.sync();                
    }
    
    boolean contains(LocO loc) throws OpFailedException {
        Bc bc = getOrCreateBc(((ContextS3) loc.context).bucketName);
        if ( bc.contains((OPath) loc.getPath()) ) return(true);
        else {
            bc.sync();
            return( bc.contains((OPath) loc.getPath()) );
        }
    }
    
    List<OPath> getList(String bucketName) throws OpFailedException {
        Bc bc = getOrCreateBc(bucketName);
        return( bc.getList() );
    }

    List<OPath> getUpdatedList(String bucketName) throws OpFailedException {
        Bc bc = createBc(bucketName);
        return( bc.getList() );
    }

    private Bc getOrCreateBc(String bucketName) {

        Bc bc = bcHt.get(bucketName);
        if ( bc == null ) {
            bc = new Bc(bucketName);
            bcHt.put(bucketName,bc);
        }
        return(bc);

    }

    private Bc createBc(String bucketName) {
        Bc bc = new Bc(bucketName);
        bcHt.put(bucketName,bc);
        return(bc);
    }
    
        // --------------------------------------------------
    
        /** Contents of one bucket         
         */
    class Bc {
        private HashMap<String,OPath> ht;
        private String bucketName;

            /** 
             * 
             * @param ps
             * @throws pojoS3.OpFailedException
             */
        public Bc(String bucketName) {                    
            this.bucketName = bucketName;       
        }              

            /** Sync with the current contents of the S3 bucket.
             * 
             * @param mops
             * @param bucketName
             */
        void sync()
            throws OpFailedException {        
            ht = new HashMap<String, OPath>();

            List<String> s3Keys = mops.listS3Objects(bucketName);

            for ( String k : s3Keys ) {
                OPath p = new OPath(k.split("/"));
                ht.put(p.toPathString(),p);
            }               
        }

        boolean contains(OPath path) throws OpFailedException {
            if ( ht == null ) {
                sync();          
            }                
            return( ht.get(path.toPathString()) != null );
        }

        List<OPath> getList() throws OpFailedException {
            if ( ht == null ) sync();
            List<OPath> lst = new ArrayList<OPath>();
            for ( Iterator<OPath> it = ht.values().iterator(); it.hasNext(); ) {
                lst.add(it.next());
            }

            Collections.sort(lst);

            return(lst);
        }                
    }
}
